package cn.itcast.bos.web.action.take_delivery;

import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.service.take_delivery.PromotionService;
import cn.itcast.bos.utils.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@Namespace("/")
@ParentPackage("json-default")
@Scope("protoType")
public class PromotionAction extends BaseAction<Promotion> {

    @Autowired
    private PromotionService promotionService;

    private File titleImgFile;
    private String titleImgFileFileName;

    public void setTitleImgFile(File titleImgFile) {
        this.titleImgFile = titleImgFile;
    }

    public void setTitleImgFileFileName(String titleImgFileFileName) {
        this.titleImgFileFileName = titleImgFileFileName;
    }
    @Action(value = "save_promotion",results = {@Result(name="success",type = "redirect",location = "./pages/take_delivery/promotion.html")})
    public String savePromotion(){
        String savePath = ServletActionContext.getServletContext().getRealPath("/upload/");
        String saveUrl = ServletActionContext.getServletContext().getContextPath()+"/upload/";
        //生成随机名称
        UUID uuid = UUID.randomUUID();
        String ext = titleImgFileFileName.substring(titleImgFileFileName.lastIndexOf("."));
        String randomImgFileName=uuid+ext;
        //将文件保存到绝对路径中
        File destFile = new File(savePath+"/"+randomImgFileName);
        try {
            FileUtils.copyFile(titleImgFile,destFile);
            //将文件的相对路径保存到model中,存入数据库
            model.setTitleImg(saveUrl+randomImgFileName);
            promotionService.save(model);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }
    //查询的方法(包含条件查询);
    @Action(value="promotion_pageQuery",results = {@Result(name="success",type = "json")})
    public String  pageQuery(){
        Pageable pageable = new PageRequest(page-1,rows);
        //构建条件查询对象
        Specification<Promotion> specification = new Specification<Promotion>() {
            @Override
            public Predicate toPredicate(Root<Promotion> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

                return null;
            }
        };

        Page<Promotion> pageData = promotionService.pageQuery(specification,pageable);
        pushPageDataToValueStack(pageData);
        return SUCCESS;
    }
   /* //修改数据,让数据回显的方法
    @Action(value="promotion_update",results = {@Result(name="success",location = "/pages/take_delivery/promotion_add.html")})
    public String updatePromotionById(){
        Promotion promotion = promotionService.findOneById(model);
        ActionContext.getContext().getValueStack().push(promotion);
        return SUCCESS;
    }*/

}
